<template>
  <el-card>
    <div>
      <!-- 1.0 查询的表单 -->
      <el-form
        :inline="true"
        :model="searchForm"
        class="demo-form-inline"
        size="mini"
        ref="searchRef"
      >
        <el-form-item label="订单号" prop="orderNo">
          <el-input
            v-model="searchForm.orderNo"
            placeholder="订单号"
          ></el-input>
        </el-form-item>
        <el-form-item label="收货人" prop="consignee">
          <el-input
            v-model="searchForm.consignee"
            placeholder="收货人"
          ></el-input>
        </el-form-item>
        <el-form-item label="手机号" prop="phone">
          <el-input v-model="searchForm.phone" placeholder="手机号"></el-input>
        </el-form-item>
        <el-form-item label="订单状态" prop="orderState">
          <el-select v-model="searchForm.orderState" placeholder="订单状态">
            <el-option label="已受理" value="已受理"></el-option>
            <el-option label="派送中" value="派送中"></el-option>
            <el-option label="已完成" value="已完成"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="选择时间" prop="date">
          <el-date-picker
            v-model="searchForm.date"
            type="datetimerange"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onSubmit">查询</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="default" @click="resetSubmit">重置</el-button>
        </el-form-item>
      </el-form>

      <!-- 2.0 渲染数据表格 -->
      <el-table :data="tableData" border style="width: 100%">
        <el-table-column fixed prop="orderNo" label="订单号"> </el-table-column>
        <el-table-column label="下单时间" width="180px">
          <template v-slot="{ row }">
            {{ row.orderTime | formatDate }}
          </template>
        </el-table-column>
        <el-table-column prop="phone" label="手机号" width="130px">
        </el-table-column>
        <el-table-column prop="consignee" label="收货人"> </el-table-column>
        <el-table-column prop="deliverAddress" label="配送地址" width="110px">
        </el-table-column>
        <el-table-column prop="deliveryTime" label="送达时间" width="180px">
          <template v-slot="{ row }">
            {{ row.deliveryTime | formatDate }}
          </template>
        </el-table-column>
        <el-table-column prop="remarks" label="用户备注"> </el-table-column>
        <el-table-column prop="orderAmount" label="订单金额">
          <template v-slot="{ row }">
            {{ row.orderAmount | frommatMoney('€') }}
          </template>
        </el-table-column>
        <el-table-column prop="orderState" label="订单状态" width="120">
        </el-table-column>
        <el-table-column fixed="right" label="操作" width="100">
          <template v-slot="{ row }">
            <el-button @click="dialogVisible = true" type="text" size="small"
              >查看</el-button
            >
            <el-button
              type="text"
              size="small"
              @click="$router.push('/order/edit/' + row.id)"
              >编辑</el-button
            >
          </template>
        </el-table-column>
      </el-table>

      <!-- 3.0 分页处理 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="currentPage"
        :page-sizes="[2, 5, 10, 15]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>

      <!-- 显示当前的数据 -->
      <el-dialog title="提示" :visible.sync="dialogVisible" width="50%">
        <span>这是一段信息</span>
        <span slot="footer" class="dialog-footer">
          <el-button @click="dialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="dialogVisible = false"
            >确 定</el-button
          >
        </span>
      </el-dialog>
    </div>
  </el-card>
</template>

<script>
import { getOrderList } from '@/api/orders.js'

export default {
  data() {
    return {
      searchForm: {
        orderNo: '',
        consignee: '',
        phone: '',
        orderState: '',
        date: ''
      },
      currentPage: 1,
      pageSize: 5,
      total: 0,
      tableData: [],
      dialogVisible: false
    }
  },
  created() {
    this.getOrders()
  },
  methods: {
    async getOrders() {
      const { data, total } = await getOrderList({
        orderNo: this.searchForm.orderNo,
        consignee: this.searchForm.consignee,
        phone: this.searchForm.phone,
        orderState: this.searchForm.orderState,
        date: JSON.stringify(this.searchForm.date),
        pageSize: this.pageSize,
        currentPage: this.currentPage
      })
      console.log(data)
      this.tableData = data
      this.total = total
    },
    handleSizeChange(val) {
      // console.log(`每页 ${val} 条`)
      this.pageSize = val
      this.getOrders()
    },
    handleCurrentChange(val) {
      // console.log(`当前页: ${val}`)
      this.currentPage = val
      this.getOrders()
    },
    // 带查询的数据请求
    onSubmit() {
      this.currentPage = 1
      this.getOrders()
    },
    resetSubmit() {
      this.$refs.searchRef.resetFields()
      this.searchForm.date = ''
      this.getOrders()
    }
  }
}
</script>

<style lang="less" scoped>
.el-pagination {
  margin-top: 30px;
}

/deep/.el-input {
  input {
    width: 120px;
  }
}
</style>
